<?php
namespace app\index\controller;
use Think\Db;
use think\Controller;
use think\Cookie;
use think\jssdk;

date_default_timezone_set("PRC");
class Wechat extends Controller{
   public function index(){
       $appid = "wxddd2363aff49a909";//获取code，回调userinfo方法
       $rurl = urlencode("http://118.193.221.147/ParallelToTheNet/public/index/Wechat/userinfo");

       $this->redirect( "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$rurl."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ");
   }
    public function userinfo(){
//        var_dump($_GET);
        //获取refresh_token
        $code = $_GET['code'];
//        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxddd2363aff49a909&secret=f9b63690e4c1acd765753d2ee99c46f3&code=".$code."&grant_type=authorization_code";
        $urls="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxddd2363aff49a909&secret=f9b63690e4c1acd765753d2ee99c46f3&code=".$code."&grant_type=authorization_code ";
        $json_ret = file_get_contents($urls);
        $result = json_decode($json_ret, true);
//        dump($result);
        //获取access_token和openid
//        $access_token = $result["refresh_token"];
//        dump($access_token);
//        $access_token = $result["refresh_token"];
//        $urlrefresh = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wxddd2363aff49a909&grant_type=refresh_token&refresh_token=".$access_token;
//        $json_ret = file_get_contents($urlrefresh);
//        $result = json_decode($json_ret, true);
//        var_dump($result);
        //拉取用户信息
//        $data = "https://api.weixin.qq.com/sns/auth?access_token=".$result["access_token"]."&openid=".$result["openid"];
        $data = "https://api.weixin.qq.com/sns/userinfo?access_token=".$result["access_token"]."&openid=".$result["openid"];
        $json_ret = file_get_contents($data);
        $result = json_decode($json_ret, true);
//        var_dump($result);
//将获取的数据存储到数据库
        $userinfo = [];
        $userinfo["openid"] = $result["openid"];
        $userinfo["nickname"] = $result["nickname"];
        $userinfo["city"] = $result["city"];
        $userinfo["sex"] = $result["sex"];
        $userinfo["url"] = $result["headimgurl"];
        $userinfo["time"] = time();
//        dump($userinfo);
        $user = db("park_user")->where("openid",$result["openid"])->find();//查询是否已经使用过微信登录
//        $phone = db("park_user")->where("openid",$user[0]["openid"])->value("phone");
//        dump($phone);
        if($user) {//曾经微信登录过
            $phone = db("park_user")->where("openid",$user["openid"])->value("phone");//查询该条数据是否绑定过手机
            if($phone==0){
                $this->assign("state",1);//跳转到绑定手机页面的标识
                $phone_id =  db("park_user")->where("openid",$user["openid"])->value("id");//查询该条数据的id
                setcookie("phone_id",$phone_id,time()+86400,"/");
//                dump($_COOKIE['phone_id']);
                return view("Index/register");
            }else{
                setcookie("username",$user["username"]);
                setcookie("nickname",$user["nickname"]);
                $date = Db::table("hotel")->where("type=0")->paginate(5);
                if($user["nickname"] != ''){
                    $name= $user["nickname"]." <a href='".url('Index/cancel')."'>注销</a>";
                }else{
                    $name=' <a href="'.url('Index/login').'">登录&注册</a>';
                }
                $data=Db::table("guide")->paginate(3);
                $page = $data->render();

                $dome = Db::name('scenic_spot')->paginate(6);
                $cc = array();
                for($i=0;$i<count($dome);$i++){
                    $cc[$i]['img'] = explode("/",$dome[$i]["img"]);
                }
//                $pge = $dome->render();

                $pge = $dome->render();
                $ass = new jssdk('wxddd2363aff49a909',"f9b63690e4c1acd765753d2ee99c46f3");
                $as=$ass->getSignPackage();
                $this -> assign('sign',$as);
                return view("Index/index", array('cc'=>$cc,"bo" => $date,"bd" => $dome,"bx"=>$data, "pge" => $pge, "page" => $page,"username"=>$name));
            }
        }else{
            $sql = db("park_user")->insertGetId($userinfo);//获取到该条数据id
            if($sql!=""){
                $this->assign("state",1);//跳转到绑定手机页面的标识
                setcookie("phone_id",$sql,time()+86400,"/");
                return view("Index/register");
            }else{
                return "服务器繁忙，请稍后再试";
            }
        }
    }

}
